package pers.javaoop.m1;

/**
 * 课程：JavaOOP ，山西应用科技学院信息工程学院计算机科学与技术专业 2201班
 *
 * @author by 2022010101张三
 * {@code @user Administrator}
 * {@code @Date 2024/12/4 7:01}
 * {@code @Description} 练习
 * TODO: Java工程编程示范：打印成绩单
 */
public class CourseGrades {
    public static void grades(){
        //Java基本变量：8种基本数据类型：byte/1，short/2，int/4*，long/8，
        // float/4，double/8*，char/1，boolean/1，
        double math=85.4;
        double english=90.2;
        int nature=80;
        String id="202001";
        String name="张三";

        //变量输出的格式控制
        System.out.printf("==%7s%4s%5d%6.1f%6.1f\n",id,
                name,nature,english,math);    //格式输出



        boolean pass=true;
        System.out.printf("==学号%7s，姓名%4s，自然%5d，英语%6.1f，" +
                "数学%6.1f，通过%5b\n",id,name,nature,english,
                math,pass);    //格式输出

        //使用随机数
        nature=(int)(Math.random()*100.0);//强制转转
        english=Math.random()*100.2;
        math=Math.random()*100.2;
        System.out.printf("%7s,%4s,%5d,%6.1f,%6.1f,%5b\n",id,name,nature,english,math,pass);

        //利用循环连续输出
        nature=(int)(Math.random()*100.0);//强制转转
        english=Math.random()*100.2;
        math=Math.random()*100.2;
        System.out.printf("%7s,%4s,%5d,%6.1f,%6.1f,%5b\n",id,name,nature,english,math,pass);
        System.out.println("======================================\n\n");


        int n=10, m=6;
        System.out.println("学号\t\t姓名\t\t数学\t\t物理\t\t语文\t\t自然\t\t图画\t\t音乐\t\t平均成绩");
        for (int i = 0; i < n; i++) {   //n个学生
            System.out.printf("%s%4s","202420"+i,"姓名"+i);
            double s=0.0;
            for (int j = 0; j < m; j++) {   //  m个科目
                double x=15.0+85.0*Math.random();  // [15,100)之间的随机小数
                System.out.printf("\t%.2f",x);
                s+=x;
            }
            System.out.printf("\t%4.2f\n",s/m);
        }

        //利用数组连续产生数据
        System.out.println("\n\n");
        double[][] grades = new double[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                grades[i][j]=15.0+Math.random()*85.0;
            }
        }

        //利用数组连续格式化输出
        for (int i = 0; i < n; i++) {
            double d=0.0;
            System.out.printf("%d%5s",2025200+i,"姓名"+i);
            for (int j = 0; j < m; j++) {
                d+=grades[i][j];
                System.out.printf("\t%5.1f",grades[i][j]);
            }
            System.out.printf("\t%5.1f%n",d/m);
        }

        //上面for技术循环可以简化为foreach循环
        System.out.println("=====替换成foreach循环=====");
        double d=0.0;
        for(double[] gr:grades){
            for(double g:gr){
                d+=g;
                System.out.printf("\t%5.1f",g);
            }
            System.out.println();
        }
        System.out.printf("\n sum=%5.2f",d);
    }

    public static void main(String[] args) {
        //常用的格式输出
        System.out.println("=============== 课程成绩 ===============");
        System.out.println("学号\t\t姓名\t数学\t物理\t\t平均成绩\n——————————————————————————————————————");
        System.out.printf("%7s%4s%5d%6.1f%6.1f\n","202001","张三",80,90.2,85.4);    //格式输出
        grades();   //调用grades方法，批量输出课程成绩
        nine();
    }
    public static void nine(){
        System.out.println("\n=============== 九九表 ===============");
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.printf("%d*%d=%d\t",j,i,i*j);
            }
            System.out.println();
        }
        for (int i = 0; i <10 ; i++) {
            for (int j = 0; j <=i ; j++) {
                System.out.printf("%d*%d=%d\t",j,i,i*j);
            }
            System.out.println();
        }
    }
}
